
package com.joysuch.wwyt.api.risk;

import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.service.BaseLoginAccountService;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.risk.bean.*;
import com.joysuch.wwyt.risk.entity.RiskDistrict;
import com.joysuch.wwyt.risk.entity.RiskDistrictEvaluation;
import com.joysuch.wwyt.risk.service.RiskDistrictEvaluationService;
import com.joysuch.wwyt.risk.service.RiskDistrictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/risk")
public class RiskAPIController {

    private static final Logger logger = LoggerFactory.getLogger(RiskAPIController.class);

    @Autowired
    private RiskDistrictService districtService;
    @Autowired
    private RiskDistrictEvaluationService districtEvaluationService;
    @Autowired
    private BaseLoginAccountService loginAccountService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;


    // 风险区域列表
    @PostMapping("/riskDistrictList")
    public Object getSimplePage(Pageable pageList) {
        try {
            // simpleList
            Page<SimpleSelectListBean> list = simpleSelectListService.page(pageList, RiskDistrict.class, "name");
            return ResultBean.pageData(list);
        } catch (Exception e) {
            logger.error("生成区域失败", e);
            return ResultBean.fail(101, "查询区域列表失败");
        }
    }

    private RiskDistrictListBean convert(RiskDistrict d) {
        RiskDistrictListBean bean = new RiskDistrictListBean();
        bean.setId(d.getId());
        bean.setCode(d.getCode());
        bean.setName(d.getName());
        bean.setCreateTime(SimpleDateFormatCache.getYmdhms().format(d.getCreateTime()));
        bean.setCreator(loginAccountService.findAccountNameById(d.getCreateBy()));
        bean.setRemark(d.getRemark());
        bean.setCoordinates(d.getCoordinates());
        return bean;
    }



    // 获取区域评估状态列表
    @RequestMapping("/evaluation-district-list")
    public Object stableRiskList(Pageable page, Integer state) {
        page = PageDataUtils.addOrderByDesc(page, "code");
        Page<RiskDistrictEvaluationListBean> pageData = districtService.findDistrictEvluationByPage(page, state);
        return ResultBean.pageData(pageData.getContent(), pageData.getTotalElements());
    }

    // 获取区域风险评估列表
    @RequestMapping("stable-risk-list")
    public Object stableRiskList(Pageable page, RiskDistrictStableRiskListSearchBean condition) {
        try {
            page = PageDataUtils.addOrderByDesc(page, "code");
            Page<RiskDistrictStableRiskListBean> pageData = districtService.findByPage(page, condition);
            return ResultBean.pageData(pageData.getContent(), pageData.getTotalElements());
        } catch (Exception e) {
            logger.error("生成区域失败", e);
            return ResultBean.fail(101, "删除区域失败");
        }
    }

    // 获取区域风险评估详情
    /**
     * 获取区域评估详情
     * 
     * @param riskDisctrictId
     *            区域ID
     * @param searchHistory
     *            true:查询历史，false:不查询历史
     * @return
     */
    @RequestMapping("get-stable-risk-evaluation-detail")
    public Object getStableRiskEvaluationDetail(Long riskDisctrictId, boolean searchHistory) {
        try {
            RiskDistrictStableRiskQueryBean data = districtService.findStableRiskEvaluationDetail(riskDisctrictId,
                    searchHistory);
            return ResultBean.success(data);
        } catch (Exception e) {
            logger.error("获取区域风险评估详情出错", e);
            return ResultBean.fail(101, "获取区域风险评估详情出错");
        }
    }


}
